package com.itnoob.service;

import com.itnoob.event.OrderStatusChangeEvent;
import com.itnoob.status.OrderStatusEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Service;

/**
 * @author zhoujun
 * @date 2025/3/20 20:42
 */
@Service
public class OrderService implements ApplicationEventPublisherAware {

    private Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 事件发布者
     */
    private ApplicationEventPublisher applicationEventPublisher;


    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.applicationEventPublisher = applicationEventPublisher;
    }


    public void changeOrderStatus(String orderId, String orderStatus) {
            // ....执行订单状态变更逻辑
        logger.info(" [changeOrderStatus] [执行订单,订单({})变为状态({})]的逻辑",orderId,orderStatus );
        //根据字符串转换成枚举
        OrderStatusEnum statusEnum = OrderStatusEnum.fromString(orderStatus);
        //发布事件
        applicationEventPublisher.publishEvent(new OrderStatusChangeEvent(this,orderId,statusEnum));
    }
}
